package com.edgclub.edg.activity;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.edgclub.edg.Constant;
import com.edgclub.edg.GuZhiApplication;
import com.edgclub.edg.R;
import com.edgclub.edg.adapter.OtherUserGroupAdapter;
import com.edgclub.edg.adapter.OtherUserHonerAdapter;
import com.edgclub.edg.adapter.OtherUserTopicAdapter;
import com.edgclub.edg.api.ApiConfig;
import com.edgclub.edg.api.FriendApi;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.UserApi;
import com.edgclub.edg.api.param.MessageParam;
import com.edgclub.edg.api.param.UserParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.model.HonorOrAlbBean;
import com.edgclub.edg.model.PostsAlbum;
import com.edgclub.edg.model.User;
import com.edgclub.edg.model.UserGroupBean;
import com.edgclub.edg.model.UserTopicBean;
import com.edgclub.edg.model.result.RunData;
import com.edgclub.edg.model.result.UserHomeData;
import com.edgclub.edg.uc.BanDeviceDialog;
import com.edgclub.edg.uc.DeleteDialog;
import com.edgclub.edg.uc.ReportDialog;
import com.edgclub.edg.uc.UnableScrollListView;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.edgclub.edg.utils.util.AnimationUtil;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.JsonUtil;
import com.edgclub.edg.utils.util.StringUtils;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.hyphenate.easeui.dao.DaoHelper;
import com.hyphenate.easeui.dao.UserBeanDao;
import com.hyphenate.easeui.model.ChatBean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/15.
 */
public class UserInfoOtherActivity extends GzBaseTooolBarActivity implements View.OnClickListener , VolleyListener{
    private OtherUserGroupAdapter otherUserGroupAdapter;
    private OtherUserTopicAdapter otherAdapter;
    private OtherUserHonerAdapter honerAdapter,imHonerAdapter;

    private RelativeLayout mSex_layout,mTrends_layout;
    private ImageView mIcon_user;
    private ImageView mVip;
    private TextView mUser_name;
    private TextView mSign;
    private LinearLayout mChat;
    private RelativeLayout mFollow;
    private TextView mLv;
    private TextView mFollow_text , tiezi , xiangce , rongyu;
    private TextView mNum_time;
    private RelativeLayout mTrends;
    private TextView mTrends_num;
    private ImageView mCache_size;
    private com.edgclub.edg.uc.UnableScrollListView mTrends_list;
    private RelativeLayout mAlbum , main_back , main_more;
    private TextView mAlbum_num;
    private ImageView mCache1_size , add;
    private com.hyphenate.easeui.widget.EaseExpandGridView mAlbum_gridview;
    private RelativeLayout mHonour;
    private TextView mHonour_num , eid;
    private ImageView mCache2_size;
    private com.hyphenate.easeui.widget.EaseExpandGridView mHonour_gridview;

    private User toUser;
    private HonorOrAlbBean honors;
    private HonorOrAlbBean albums;
    private UserTopicBean posts;
    private boolean isChat=false;
    private boolean isload=false;
    private boolean isOne=false;

    private RelativeLayout groups_layout;
    private TextView group_num;
    private TextView group_text;
    private UnableScrollListView group_list;
    private UserGroupBean groupBean;

    private String banFlag = "0";

    private RelativeLayout auth_layout;
    private ImageView arrow_left , arrow_right;
    private RelativeLayout arrow_left_layout , arrow_right_layout;
    private TextView auth_text;
    private RelativeLayout mBg_user;
    private RelativeLayout auth_bg;

    @Override
    public int getContentView() {
        return R.layout.activity_other;
    }

    @Override
    public void setTitleBarView() {
        setStatusBarHeight(true);
    }

    @Override
    public void initView() {
        auth_bg = (RelativeLayout) findViewById(R.id.auth_bg);
        mBg_user = (RelativeLayout) findViewById(R.id.bg_user);
        auth_text = (TextView) findViewById(R.id.auth_text);
        arrow_left_layout  = (RelativeLayout) findViewById(R.id.arrow_left_layout);
        arrow_left = (ImageView) findViewById(R.id.arrow_left);
        arrow_left.setImageResource(R.drawable.loading_arrow_left);
        AnimationDrawable animationDrawable = (AnimationDrawable) arrow_left.getDrawable();
        animationDrawable.start();
        arrow_left_layout.setOnClickListener(this);

        arrow_right_layout  = (RelativeLayout)findViewById(R.id.arrow_right_layout);
        arrow_right = (ImageView) findViewById(R.id.arrow_right);
        arrow_right.setImageResource(R.drawable.loading_arrow_right);
        AnimationDrawable animationDrawable2 = (AnimationDrawable) arrow_right.getDrawable();
        animationDrawable2.start();
        arrow_right_layout.setOnClickListener(this);
        auth_layout = (RelativeLayout) findViewById(R.id.auth_layout);


        groups_layout = (RelativeLayout) findViewById(R.id.groups_layout);
        group_num = (TextView) findViewById(R.id.group_num);
        group_text = (TextView) findViewById(R.id.group_text);
        group_list = (UnableScrollListView) findViewById(R.id.group_list);

        mTrends_layout= (RelativeLayout) findViewById(R.id.trends_layout);
        mSex_layout = (RelativeLayout) findViewById(R.id.sex_layout);
        mIcon_user = (ImageView) findViewById(R.id.icon_user);
        mVip = (ImageView) findViewById(R.id.vip);
        mUser_name = (TextView) findViewById(R.id.user_name);
        mSign = (TextView) findViewById(R.id.sign);
        mChat = (LinearLayout) findViewById(R.id.chat_layout);
        mFollow = (RelativeLayout) findViewById(R.id.follow_layout);
        mFollow_text = (TextView) findViewById(R.id.follow_text);
        mLv = (TextView) findViewById(R.id.lv);
        mNum_time = (TextView) findViewById(R.id.num_time);
        tiezi = (TextView) findViewById(R.id.tiezi);
        xiangce = (TextView) findViewById(R.id.xiangce);
        rongyu = (TextView) findViewById(R.id.rongyu);
        mTrends = (RelativeLayout) findViewById(R.id.trends);
        mTrends_num = (TextView) findViewById(R.id.trends_num);
        mCache_size = (ImageView) findViewById(R.id.cache_size);
        mTrends_list = (com.edgclub.edg.uc.UnableScrollListView) findViewById(R.id.trends_list);
        mAlbum = (RelativeLayout) findViewById(R.id.album);
        mAlbum_num = (TextView) findViewById(R.id.album_num);
        mCache1_size = (ImageView) findViewById(R.id.cache1_size);
        mAlbum_gridview = (com.hyphenate.easeui.widget.EaseExpandGridView) findViewById(R.id.album_gridview);
        mHonour = (RelativeLayout) findViewById(R.id.honour);
        mHonour_num = (TextView) findViewById(R.id.honour_num);
        eid = (TextView) findViewById(R.id.eid);
        mCache2_size = (ImageView) findViewById(R.id.cache2_size);
        main_back = (RelativeLayout) findViewById(R.id.main_back);
        main_more = (RelativeLayout) findViewById(R.id.main_more);
        add = (ImageView) findViewById(R.id.add);
        mHonour_gridview = (com.hyphenate.easeui.widget.EaseExpandGridView) findViewById(R.id.honour_gridview);

        toUser= (User) getIntent().getSerializableExtra("user");
        isChat=getIntent().getBooleanExtra("chat",false);
        isOne=getIntent().getBooleanExtra("one",false);
        if(null==toUser)toUser=new User();

        if(toUser.getId().equals(GuZhiApplication.getInstance().getUserId())){
            mChat.setVisibility(View.GONE);
            mFollow.setVisibility(View.GONE);
        }
        mChat.setOnClickListener(this);
        mFollow.setOnClickListener(this);
        mTrends_layout.setOnClickListener(this);
        findViewById(R.id.photo_layout).setOnClickListener(this);
        findViewById(R.id.honour_layout).setOnClickListener(this);
        main_back.setOnClickListener(this);
        main_more.setOnClickListener(this);
    }

    @Override
    public void initData() {
        otherUserGroupAdapter = new OtherUserGroupAdapter(this);
        otherAdapter=new OtherUserTopicAdapter(this);
        honerAdapter=new OtherUserHonerAdapter(this,true);
        imHonerAdapter=new OtherUserHonerAdapter(this,false);
        setData();
        getHomepage();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.chat_layout:
                if(isChat&&isOne){
                        finish();
                }else {
                    if(!"".equals(toUser.getImid())) {
                        ChatBean chatBean = RunData.getChatBeanMode(null, toUser);
                        Intent intent = new Intent(this, ChatActivity.class);
                        intent.putExtra("chatBean", chatBean);
                        startActivity(intent);
                    }
                }

                break;
            case R.id.follow_layout:
                if("关注".equals(mFollow_text.getText()+"")){
                    subscribe(true);
                }else{
                    subscribe(false);
                }
                break;
            case R.id.trends_layout:
                Intent intent=new Intent(this,UserInfoActionOrPhotoActivity.class);
                intent.putExtra("user",toUser);
                startActivity(intent);
                break;
            case  R.id.photo_layout:
                Intent intent1=new Intent(this,UserInfoOtherPhotoActivity.class);
                intent1.putExtra("user",toUser);
                startActivity(intent1);
                break;
//            case R.id.honour_layout:
//                Intent intent2=new Intent(this,UserInfoHonourActivity.class);
//                intent2.putExtra("user",toUser);
//                startActivity(intent2);
//                break;
            case R.id.main_back:
                finish();
                break;
            case R.id.main_more:
                boolean isBanDevice = new UserPrefUtils(this).isBanDevice();
                if (!isBanDevice){
//                    final ReportDialog dialog = new ReportDialog(
//                            UserInfoOtherActivity.this);
//                    dialog.setCanceledOnTouchOutside(true);
//                    dialog.show();
//                    dialog.setSelectDialogClickListener(new ReportDialog.SelectDialogClickListener() {
//                        @Override
//                        public void report() {
//                            dialog.dismiss();
//                            Intent intent = new Intent(UserInfoOtherActivity.this , GzPersonReportActivity.class);
//                            intent.putExtra("user" , toUser);
//                            UserInfoOtherActivity.this.startActivity(intent);
//                        }
//                    });
                } else {
                    final BanDeviceDialog dialog = new BanDeviceDialog(
                            UserInfoOtherActivity.this);
                    dialog.setCanceledOnTouchOutside(true);
                    dialog.show();
                    dialog.setSelectDialogClickListener(new BanDeviceDialog.SelectDialogClickListener() {
                        @Override
                        public void banaccount() {
                            dialog.dismiss();
                            banFlag = "0";
                            showBanDialog();
                        }

                        @Override
                        public void bandevice() {
                            dialog.dismiss();
                            banFlag = "1";
                            showBanDialog();
                        }
                    });
                }
                break;
            case R.id.arrow_left_layout:
                mBg_user.setVisibility(View.VISIBLE);
                Animation animation1 = AnimationUtil.inFromLeftAnimation();
                mBg_user.setAnimation(animation1);
                animation1.startNow();
                auth_layout.setVisibility(View.GONE);
                auth_bg.setVisibility(View.GONE);
                Animation animation2 = AnimationUtil.inToRightAnimation();
                auth_layout.setAnimation(animation2);
                animation2.startNow();
                break;
            case R.id.arrow_right_layout:
                auth_layout.setVisibility(View.VISIBLE);
                auth_bg.setVisibility(View.VISIBLE);
                Animation animation3 = AnimationUtil.inFromRightAnimation();
                auth_layout.setAnimation(animation3);
                animation3.startNow();
                mBg_user.setVisibility(View.GONE);
                Animation animation4 = AnimationUtil.outToLeftAnimation();
                mBg_user.setAnimation(animation4);
                animation4.startNow();
                break;
        }
    }


    private void setData(){
        if (null == honors) {
            honors = new HonorOrAlbBean();
        }
        if (null == albums) {
            albums = new HonorOrAlbBean();
        }
        if (null == posts) {
            posts = new UserTopicBean();
        }
        if (null == groupBean) {
            groupBean = new UserGroupBean();
        }
        if(0==toUser.getSex()){
            mSex_layout.setBackgroundResource(R.drawable.girl_box);
        }else{
            mSex_layout.setBackgroundResource(R.drawable.boy_box);
        }
        new GlideUtils().Glidde_loadCropCircleImage(this,toUser.getLogo(),mIcon_user);

        if (toUser.getUserAuth() != null && !toUser.getUserAuth().getAuthentication().equals("")){
            arrow_right_layout.setVisibility(View.VISIBLE);
            auth_text.setText(toUser.getUserAuth().getAuthentication());
        } else {
            arrow_right_layout.setVisibility(View.GONE);
        }

        mIcon_user.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(UserInfoOtherActivity.this,
                        ImageViewPagerActivity.class);
                List<PostsAlbum> imageList=new ArrayList<>();
                PostsAlbum postsAlbum = new PostsAlbum();
                postsAlbum.setUrl(toUser.getLogo()+"");
                imageList.add(postsAlbum);
                intent.putExtra("urls", (Serializable) imageList);
                intent.putExtra("position", "0");
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                (UserInfoOtherActivity.this).overridePendingTransition(R.anim.head_in, R.anim.head_out);
            }
        });

        if(null!=toUser.getUserAuth()){
            if ("-1".equals(toUser.getUserAuth().getType())){
                mVip.setVisibility(View.GONE);
            }
            if("0".equals(toUser.getUserAuth().getType())){
                mVip.setVisibility(View.VISIBLE);
                mVip.setImageResource(R.drawable.v_huan);
            }else if("1".equals(toUser.getUserAuth().getType())){
                mVip.setVisibility(View.VISIBLE);
                mVip.setImageResource(R.drawable.v_lan);
            }
        }else{
            mVip.setVisibility(View.GONE);
        }
        eid.setText("Eid:"+toUser.getGzno());
        mLv.setText("Lv."+toUser.getLevel());
        mUser_name.setText(toUser.getNick());
        if (toUser.getIntro().equals("")){
            mSign.setText("警察蜀黍，就是这个人不写签名");
        }else{
            mSign.setText(toUser.getIntro());
        }
        mNum_time.setText("被赞"+toUser.getLikedCount()+"次");
        if(toUser.getInteractState()==0){
            mFollow_text.setText("关注");
            add.setVisibility(View.VISIBLE);
        }else if(toUser.getInteractState()==10){
            mFollow_text.setText("已关注");
            add.setVisibility(View.GONE);
        }else if(toUser.getInteractState()==3){
            mFollow_text.setText("相互关注");
            add.setVisibility(View.GONE);
        }


        mTrends_num.setText(posts.getCount()+"");
        otherAdapter.setList(posts.getList());
        mTrends_list.setAdapter(otherAdapter);
        mAlbum_num.setText(albums.getCount()+"");
        honerAdapter.setList(albums.getList());
        mAlbum_gridview.setAdapter(honerAdapter);
        mHonour_num.setText(honors.getCount()+"");
        imHonerAdapter.setList(honors.getList());
        mHonour_gridview.setAdapter(imHonerAdapter);

        group_num.setText(groupBean.getCount()+"");
        otherUserGroupAdapter.setList(groupBean.getList());
        group_list.setAdapter(otherUserGroupAdapter);
    }
    private void getHomepage(){
        if(isload)return;
        UserParam userParam=new UserParam();
        userParam.setUserId(new UserPrefUtils(this).getUser().getId());
        userParam.setToUserId(toUser.getId());
        new UserApi(this).getotherhomepage(userParam,this);
    }
    private void subscribe( boolean res){
        if(isload)return;
        MessageParam messageParam=new MessageParam();
        messageParam.setUserId(GuZhiApplication.getInstance().getUserId());
        messageParam.setToUserId(toUser.getId());
        if(res) {
            messageParam.setTarget("add");
            new FriendApi(this).getSubscribe(messageParam, this);
        }else {
            messageParam.setTarget("delete");
            new FriendApi(this).getSubscribe(messageParam, this);
        }
    }

    @Override
    public void onStart(String tag) {
        isload=true;
        showDialog("请求中，请稍后...",false);
    }

    @Override
    public void onSuccess(String response, String tag) {
        dismissDialog();
        isload=false;
        if (JSONHelper.isSuccess(response)) {
            if(tag.equals(ApiConfig.APIURL.URL_HOMEPAGE)||tag.equals(ApiConfig.APIURL.URL_OTHERHOMEPAGE)) {
                UserHomeData userData = JsonUtil.getMode(response, UserHomeData.class);
                if (null != userData && null != userData.getData() && null != userData.getData().getUser()) {
                        toUser = userData.getData().getUser();
                    if (!DaoHelper.getInstance(UserInfoOtherActivity.this, Constant.DB_NAME_CHAT).isHas(UserBeanDao.Properties.Imid, toUser.getImid())) {
                        DaoHelper.getInstance(UserInfoOtherActivity.this, Constant.DB_NAME_CHAT).insert(toUser);
                    }
                    if (!DaoHelper.getInstance(UserInfoOtherActivity.this, Constant.DB_NAME_CHAT).isHas(UserBeanDao.Properties.Imid, toUser.getImid())) {
                        DaoHelper.getInstance(UserInfoOtherActivity.this, Constant.DB_NAME_CHAT).insert(toUser);
                    } else {
                        DaoHelper.getInstance(UserInfoOtherActivity.this, Constant.DB_NAME_CHAT).update(toUser);
                    }

                    honors = userData.getData().getHonors();

                    albums = userData.getData().getAlbums();

                    posts = userData.getData().getPosts();
                    if (posts==null || posts.getList().size()<1){
                        tiezi.setVisibility(View.VISIBLE);
                        mTrends_list.setVisibility(View.GONE);
                    }else{
                        tiezi.setVisibility(View.GONE);
                        mTrends_list.setVisibility(View.VISIBLE);
                    }
                    if (albums==null ||albums.getList().size()<1){
                        xiangce.setVisibility(View.VISIBLE);
                        mAlbum_gridview.setVisibility(View.GONE);
                    }else{
                        xiangce.setVisibility(View.GONE);
                        mAlbum_gridview.setVisibility(View.VISIBLE);
                    }
                    String srt = "0";
                    if(honors!=null){
                        srt = honors.getCount().toString().trim();
                    }
                    if ("0".equals(srt)){
                        rongyu.setVisibility(View.VISIBLE);
                        mHonour_gridview.setVisibility(View.GONE);
                    }else{
                        rongyu.setVisibility(View.GONE);
                        mHonour_gridview.setVisibility(View.VISIBLE);
                    }
                    groupBean = userData.getData().getGroups();
                    if (groupBean==null){
                        group_text.setVisibility(View.VISIBLE);
                        group_list.setVisibility(View.GONE);
                    }else{
                        if (groupBean.getList()!= null && groupBean.getList().size() > 0){
                            group_text.setVisibility(View.GONE);
                            group_list.setVisibility(View.VISIBLE);
                        } else {
                            group_text.setVisibility(View.VISIBLE);
                            group_list.setVisibility(View.GONE);
                        }
                    }
                    setData();
                }

            }else if(tag.equals(ApiConfig.APIURL.URL_SUBSCRIBE)){
                    RunData data = JsonUtil.getMode(response,
                            RunData.class);
                        if("0".equals(data.getData())){
                            toUser.setInteractState(Integer.parseInt(data.getData()));
                            mFollow_text.setText("关注");
                            add.setVisibility(View.VISIBLE);
                        }else if("10".equals(data.getData())){
                            toUser.setInteractState(Integer.parseInt(data.getData()));
                            mFollow_text.setText("已关注");
                            add.setVisibility(View.GONE);
                        }else if("3".equals(data.getData())){
                            toUser.setInteractState(Integer.parseInt(data.getData()));
                            mFollow_text.setText("相互关注");
                            add.setVisibility(View.GONE);
                        }else {
                            StringUtils.postToast(UserInfoOtherActivity.this,data.getData()+"");
                        }

            }else if(tag.equals(ApiConfig.APIURL.URL_FORBIDDEN)){
                Toast.makeText(UserInfoOtherActivity.this , "请求成功" , Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onFaile(VolleyError volleyError, String tag) {
        isload=false;
        dismissDialog();
        ResultCode.toastVolleyError(this, volleyError);
    }

    private void showBanDialog() {
        final DeleteDialog dialog = DeleteDialog.create(this, true);
        dialog.setTitle("提示");
        if (banFlag.equals("0")){
            dialog.setContent("您正在使用管理员功能禁止这个用户账号登录，生效后该账号将无法登录，请谨慎操作");
        } else {
            dialog.setContent("您正在使用管理员功能禁止这个用户设备登录，生效后该账号将无法登录，请谨慎操作");
        }
        dialog.setCanceledOnTouchOutside(true);
        dialog.setTvOkColor(getResources().getColor(R.color.c1));
        dialog.show();
        dialog.setCancelListener(new DeleteDialog.CancelClickListener() {

            @Override
            public void cancelClick() {
                dialog.dismiss();
            }
        });

        dialog.setSubmitListener(new DeleteDialog.SubmitClickListener() {

            @Override
            public void submitClick() {
                dialog.dismiss();
                UserParam userParam=new UserParam();
                userParam.setFromUserId(new UserPrefUtils(UserInfoOtherActivity.this).getUser().getId());
                userParam.setToUserId(toUser.getId());
                userParam.setType(banFlag);
                new UserApi(UserInfoOtherActivity.this).forbidden(userParam,UserInfoOtherActivity.this);
            }
        });
    }
}
